package com.lc.app.androidfirst.userdefine.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * Created by LC on 12/19/2015.
 */
public class DrawPicture extends View {
    private int view_width = 0;
    private int view_height = 0;
    private float preX = 0;
    private float preY = 0;
    private Path path;
    public Paint paint = null;
    public Bitmap cacheBitmap = null;
    public Canvas cacheCanvas = null;

    public DrawPicture(Context context, AttributeSet attrs) {
        super(context, attrs);
        view_width = context.getResources().getDisplayMetrics().widthPixels;
        view_height = context.getResources().getDisplayMetrics().heightPixels;

        cacheBitmap = Bitmap.createBitmap(view_width,view_height, Bitmap.Config.ARGB_8888);
        cacheCanvas =  new Canvas();
        cacheCanvas.setBitmap(cacheBitmap);
        path = new Path();
        paint = new Paint(Paint.DITHER_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(1);
        paint.setAntiAlias(true);
        paint.setDither(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xFFFFFFFF);
        Paint bmpPaint = new Paint();
        canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
        canvas.drawPath(path, paint);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path.moveTo(x,y);
                preX = x;
                preY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                float dx = Math.abs(x-preX);
                float dy = Math.abs(x-preY);
                if(dx >= 5 || dy >=5){
                    path.quadTo(preX,preY,(x+preX)/2,(y+preY)/2);
                    preX = x ;
                    preY = y;
                }
                break;
            case MotionEvent.ACTION_UP:
                cacheCanvas.drawPath(path, paint);
                path.reset();
                break;
        }
        invalidate();
        return true;
    }

    public void clear(){
        paint.setColor(Color.TRANSPARENT);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        paint.setStrokeWidth(50);

    }

    public void save(){
        try{
            saveBimap("mypicture"+ new Date().getTime());
            Log.d("save picture;", "all right-------");
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public void saveBimap(String fileName) throws IOException{
        File file = new File("/sdcard/"+fileName+".png");
        file.createNewFile();
        FileOutputStream fileOS= new FileOutputStream(file);
        cacheBitmap.compress(Bitmap.CompressFormat.PNG,100,fileOS);
        fileOS.flush();
        fileOS.close();
    }
}
